var app=getApp();
Page({

  /**
   * 页面的初始数据
   *  {
        id:'wenxue',
        title:'文学'
      },
      {
         id:'qingchun',
        title:'青春'
      },
      {
        id:'xiaoshuo',
        title:'小说'
      } ,
      {
        id:'yishu',
        title:'艺术'
      },
      {
        id:'lvyou',
        title:'旅游'
      },
      {
       id:'shenghuo',
       title:'生活'
      },
      {
        id:'tiyu',
        title:'体育'
      },
      {
        id:'jinji',
        title:'经济'
      },
      {
        id:'falv',
        title:'法律'
      } ,
      {
        id:'guji',
        title:'古籍'
      },
      {
        id:'zhuanji',
        title:'传记'
      } ,
      {
        id:'dongman',
        title:'动漫/幽默'
      },
      {
        id:'kepu',
        title:'科普读物'
      },
      {
        id:'kexue',
        title:'科学技术'
      },
      {
        id:'zhongxiao',
        title:'中小学教辅'

      },
      {
        id:'zhexue',
        title:'哲学/宗教'
      }
   */
  data: {
    booksArray:[],
    tableArray:[],
    id:'',
    index:'',
    pageNo:1,
    pageSize:10,
    page:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    wx.request({
      url: app.globalData.baseUrl+'onlinereading/catalog',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log(res)
        
        for(var i=0;i<res.data.data.length;i++){
          var id = 'booksArray['+i+'].id'
          var title='booksArray['+i+'].title'
          that.setData({
            [id]:res.data.data[i].id,
            [title]:res.data.data[i].name
          })
  
        }
       
      }
    })
  },
  getBookByCataId:function(id){
    var that=this
    wx.request({
      url: app.globalData.baseUrl + 'onlinereading/article',
      data: {
        catalogId: id,
        pageNo: 1,
        pageSize: 10
      },
      method: 'GET',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        that.setData({
          tableArray: []
        })
        console.log('首次打开阅读列表')
        console.log(res)
        for (var index = 0; index < res.data.data.data.length; index++) {
          var coverImg = 'tableArray[' + index + '].coverImg'
          var title = 'tableArray[' + index + '].book'
          var pdfUrl = 'tableArray[' + index + '].url'
          var id = 'tableArray[' + index + '].id'
          var imgUrl = res.data.data.data[index].coverImg
          // var img=imgUrl.substring(1,imgUrl.length)
          var pdfUrl1 = res.data.data.data[index].file
          // var pdf=pdfUrl1.substring(1,pdfUrl1.length)
          // console.log(pdf)
          that.setData({
            //  [coverImg]:res.data.data.data[index].coverImg,
            // index:index,
            [coverImg]: res.data.data.data[index].coverImg,
            [title]: res.data.data.data[index].name,
            [id]: res.data.data.data[index].id,
            [pdfUrl]: res.data.data.data[index].file
          })
        }
      }
    })
  },
  getId:function(res){
    var that=this
  
    var id=res.detail.id
    that.setData({
      id:id,
    })
    this.getBookByCataId(id)
 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  readBook:function(event){

    wx.showLoading({
      title: '正在打开书籍',
      mask:true
    })
    console.log(event)
    var url=event.currentTarget.dataset.name
    let type=url.split('.')[2]
    console.log(type)
    wx.downloadFile({
      url:url,
      success:function(res){
        console.log(res)
        var path=res.tempFilePath
        wx.openDocument({
          filePath: path,
          fileType:type,
          success:function(res){
            wx.hideLoading()
            wx.showToast({
              title: '打开书籍成功',
            })
          },
          fail:function(res){
            wx.hideLoading()
            console.log('打开书籍失败')
            console.log(res)
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBookByCataId(0)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },
  getMoreBook:function(page){
    console.log('下拉刷新')
    var that=this
    wx.request({
      url: app.globalData.baseUrl + 'onlinereading/article',
      data: {
        catalogId: that.data.id,
        pageNo: page,
        pageSize: 10
      },
      method: 'GET',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log('下拉刷新成功')
        console.log(res)
        if(res.data.data.data.length==0){
          wx.showToast({
            icon:'none',
            title: '暂时没有更多数据',
          })
        } else if (res.data.data.data.length <10){
          for (var index = 10 * (page - 1); index <10*(page-1)+res.data.data.length; index++) {
            var coverImg = 'tableArray[' + index + '].coverImg'
            var title = 'tableArray[' + index + '].book'
            var pdfUrl = 'tableArray[' + index + '].url'
            var id = 'tableArray[' + index + '].id'
            that.setData({
              //  [coverImg]:res.data.data.data[index].coverImg,
              // index:index,
              pageNo: page,
              page:page-3,
              [coverImg]: res.data.data.data[index % 10].coverImg,
              [title]: res.data.data.data[index % 10].name,
              [id]: res.data.data.data[index % 10].id,
              [pdfUrl]: res.data.data.data[index % 10].file
            })
          }
        }else{
          for (var index = 10 * (page - 1); index < 10 * page ; index++) {
            var coverImg = 'tableArray[' + index + '].coverImg'
            var title = 'tableArray[' + index + '].book'
            var pdfUrl = 'tableArray[' + index + '].url'
            var id = 'tableArray[' + index + '].id'
            that.setData({
              //  [coverImg]:res.data.data.data[index].coverImg,
              // index:index,
              pageNo: page,
              page: page,
              [coverImg]: res.data.data.data[index % 10].coverImg,
              [title]: res.data.data.data[index % 10].name,
              [id]: res.data.data.data[index % 10].id,
              [pdfUrl]: res.data.data.data[index % 10].file
            })
          }
        }
       
    
    }
    })
},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('用户下拉刷新阅读列表')
    var p=this.data.pageNo+1
    console.log(p)
    console.log(this.data.page)
    if(p-this.data.page<=2){
     
      this.getMoreBook(p)  
    }else{
      wx.showToast({
        title: '暂无更多数据',
        icon:'none',
        duration:1000
      })
    }
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})